package com.xxxx.util;

import com.google.gson.JsonObject;
import com.xxxx.test.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;

/**
 * Created by chengxia on 2018/12/5.
 */
public class dandanlogin {

    private static HttpClient httpClient = new DefaultHttpClient();
    private static HttpPost httppost;
    private static HttpResponse response;
    private HttpEntity entity;
    private String postResult = null;
    public JsonObject post(){

        JsonObject object=new JsonObject();
        String loginURL = "https://api.acplay.net/api/v2/login";
        // 创建一个httppost请求
        httppost = new HttpPost(loginURL);
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("userName", "wonimagiao");
        jsonParam.put("password","xw010908");
        jsonParam.put("appId","ezskyxyz");
        long timestamp = System.currentTimeMillis()/1000;


        System.out.println(timestamp);
//        a是请求的hash，计算方法参见官方文档
        String a="ezskyxyzxw010908"+timestamp+"wonimagiaoH9GyRm6oFY1ppukjCH7z6WJwG23jGqRZ";
        String hash= test.MD5(a);
        jsonParam.put("unixTimestamp",timestamp);
        jsonParam.put("password",a);

        try {

            StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");// 解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httppost.setEntity(entity);
            response = httpClient.execute(httppost);
            String strResult = EntityUtils.toString(response.getEntity());
            JSONObject json=new JSONObject(strResult);
            System.out.println("提取用户ID：" + json.getJSONObject("userID"));


        } catch (Exception e) {
            e.printStackTrace();
        }

        httppost.releaseConnection();
        return object;
    }

    public static void main(String[] args) {

        JsonObject object=new JsonObject();
        String loginURL = "https://api.acplay.net/api/v2/login";
        // 创建一个httppost请求
        httppost = new HttpPost(loginURL);
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("userName", "wonimagiao");
        jsonParam.put("password","xw010908");
        jsonParam.put("appId","ezskyxyz");
        long timestamp = System.currentTimeMillis()/1000;

        System.out.println(timestamp);
        String a="ezskyxyzxw010908"+timestamp+"wonimagiaoH9GyRm6oFY1ppukjCH7z6WJwG23jGqRZ";
        String hash= test.MD5(a);
        jsonParam.put("unixTimestamp",timestamp);
        jsonParam.put("hash",hash);

        try {

            StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");// 解决中文乱码问题
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httppost.setEntity(entity);
            response = httpClient.execute(httppost);
            String strResult = EntityUtils.toString(response.getEntity());
            System.out.println("查看返回的结果：" + strResult);


        } catch (Exception e) {
            e.printStackTrace();
        }

        httppost.releaseConnection();

    }

}